package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.Area;
import com.addplus.townmall.server.api.model.town.expand.AreaExpand;
import com.addplus.townmall.server.api.model.town.expand.AreaExpandWebExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 区域表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface AreaMapper extends BaseAddMapper<Area> {

    @Select("SELECT id,area_name,pid FROM area WHERE FIND_IN_SET(#{areaId},path) AND is_delete = 0 AND dept in (1,2,3)")
    List<AreaExpand> getAreaDeptThreeList(@Param("areaId") Integer areaId);

    @Select("SELECT id,area_name,pid FROM area WHERE is_delete = 0 AND dept in (1,2,3)")
    List<AreaExpand> getAreaDeptThreeListAll();

    @Select("SELECT id,area_name,pid FROM area WHERE FIND_IN_SET(#{areaId},path) AND is_delete = 0 AND dept = 4 ORDER BY sort")
    List<Area> getAreaStreetList(@Param("areaId") Integer areaId);

    @Select("SELECT id,area_name,short_area_name,dept,is_root,municipality,sort,path,longitude,latitude,is_delete,gmt_modified,pid FROM area WHERE area.is_delete=0 AND FIND_IN_SET(#{id},area.path) ORDER BY sort")
    List<AreaExpand> selectAreaTreeById(@Param("id") Integer id);

    @Select("SELECT id,area_name,short_area_name,dept,is_root,municipality,sort,path,longitude,latitude,is_delete,gmt_modified,pid FROM area WHERE area.is_delete=0 AND pid=#{pid} ORDER BY sort")
    List<AreaExpand> selectAreaListByPid(@Param("pid") Integer pid);

    @Select("SELECT id,area_name,short_area_name,dept,is_root,municipality,sort,path,longitude,latitude,is_delete,gmt_modified,pid FROM area WHERE is_delete = 0 AND area_name like CONCAT('%','${area_name}','%' )")
    List<AreaExpand> selectAreaByDept(Page page,@Param("area_name") String area_name);

    @Select("SELECT id FROM area where FIND_IN_SET(#{id},area.path) AND is_delete = 0")
    List<Integer> getAreaChildren(@Param("id") Integer id);

    @Update("UPDATE area set is_delete =1 where FIND_IN_SET(#{id},area.path) AND is_delete =0")
    Integer updateLogicallyDeleteChilders(@Param("id") Integer id);

    @Select("SELECT id,area_name,short_area_name,dept,pid,sort,path,municipality,longitude,latitude,gmt_modified FROM area WHERE is_delete = 0 AND  area_name LIKE CONCAT( '%', #{name}, '%' ) ORDER BY sort")
    List<Area> searchAreaByName(Page page,@Param("name") String name);


    @Select("<script>\n" +
            "SELECT\n" +
            " id,\n" +
            " area_name,\n" +
            " short_area_name,\n" +
            " dept,\n" +
            " is_root,\n" +
            " municipality,\n" +
            " sort,\n" +
            " path,\n" +
            " longitude,\n" +
            " latitude,\n" +
            " is_delete,\n" +
            " gmt_modified,\n" +
            " pid \n" +
            "FROM\n" +
            " area \n" +
            "WHERE\n" +
            " area.is_delete = 0 \n" +
            " AND path in \n" +
            " <foreach collection='areaIdList' open='('  item='item'  separator=',' close=')'> #{item}</foreach> " +
            " AND dept !=4 " +
            " ORDER BY dept,sort\n" +
            "</script>")
    List<AreaExpand> getAreaChilderByAreaId(@Param("areaIdList") List<Integer> areaIdList);

    @Select("<script>\n" +
            "SELECT\n" +
            " id,\n" +
            " area_name,\n" +
            " short_area_name,\n" +
            " dept,\n" +
            " is_root,\n" +
            " municipality,\n" +
            " sort,\n" +
            " path,\n" +
            " longitude,\n" +
            " latitude,\n" +
            " is_delete,\n" +
            " gmt_modified,\n" +
            " pid \n" +
            "FROM\n" +
            " area \n" +
            "WHERE\n" +
            " area.is_delete = 0 \n" +
            " AND path like concat('%',#{path},'%') \n"+
            " ORDER BY dept,sort\n" +
            "</script>")
    List<AreaExpand> getAreaChilderByPath(@Param("path") String path);

    @Select("SELECT id value,area_name title,short_area_name,dept,pid\n" +
            "FROM area\n" +
            "WHERE dept = #{dept} AND is_delete = 0")
    List<AreaExpandWebExpand> getAreaExpandWebExpandByDept(Integer dept);

    @Select("SELECT id value,area_name title,short_area_name,dept,pid\n" +
            "FROM area\n" +
            "WHERE pid = #{pid} AND is_delete = 0")
    List<AreaExpandWebExpand> getAreaExpandWebExpandByPid(Integer pid);
}
